package com.netted.operationassistant.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.hardware.Camera;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.mylhyl.superdialog.SuperDialog;
import com.netted.operationassistant.R;
import com.netted.operationassistant.entity.Port;
import com.netted.operationassistant.fragment.MyCaptureFragment;
import com.netted.operationassistant.fragment.MyCodeUtils;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;
import com.uuzuche.lib_zxing.camera.CameraManager;

import java.io.IOException;

/**
 * 二维码
 */
public class QrCodeActivity extends BaseActivity {
    private MyCaptureFragment captureFragment;
    private TextView ssTv;
    Handler handler;
    private boolean isStop = false;
    private String from;
    private RelativeLayout bxhjLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr_code);
        handler = new Handler();
        ssTv = (TextView)findViewById(R.id.qrcode_second);
        bxhjLayout = (RelativeLayout)findViewById(R.id.qrcode_dibu_layout);

        from = getIntent().getStringExtra("from");
        String tips = "";
        if ("ChargeActivity".equals(from)){
            tips = "请打开平板【我要充电】APP，将二维码放入机柜扫描区域内即可。";
        }else if ("Charge2Activity".equals(from)){
            tips = "请打开平板【我要充电】APP，将二维码放入机柜扫描区域内即可。";
        }else if("returnDeviceActivity".equals(from)){
            tips = "请打开平板【我要还机】APP检测平板后，将二维码放入机柜扫描区域内即可。";
            bxhjLayout.setVisibility(View.VISIBLE);
        }
        showDialog("提示", tips,"我知道了", new SuperDialog.OnClickPositiveListener() {
            @Override
            public void onClick(View v) {
                captureFragment = new MyCaptureFragment();
                // 为二维码扫描界面设置定制化界面
                MyCodeUtils.setFragmentArgs(captureFragment, R.layout.qc_code_view);
                captureFragment.setAnalyzeCallback(analyzeCallback);
                getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container, captureFragment).commit();

                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                timeThread.start();
            }
        });
    }



    MyCodeUtils.AnalyzeCallback analyzeCallback = new MyCodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
            bundle.putString(CodeUtils.RESULT_STRING, result);
            resultIntent.putExtras(bundle);
            QrCodeActivity.this.setResult(RESULT_OK, resultIntent);
            QrCodeActivity.this.finish();
        }

        @Override
        public void onAnalyzeFailed() {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
            bundle.putString(CodeUtils.RESULT_STRING, "");
            resultIntent.putExtras(bundle);
            QrCodeActivity.this.setResult(RESULT_OK, resultIntent);
            QrCodeActivity.this.finish();
        }
    };

    @Override
    protected void onStop() {
        super.onStop();
        stopSound();
        isStop = true;
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    Thread timeThread = new Thread(new Runnable() {
        @Override
        public void run() {
            int i = 0;

            while(i <= 20 && !isStop){
                final int count = i;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ssTv.setText((20-count)+"s");
                    }
                });
                try{
                    Thread.sleep(1000);
                    i ++;
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if(i >= 20){
                exitActivity(null);
            }
        }
    });

    public void exitActivity(View view){

        Intent intent = null;
        if ("ChargeActivity".equals(from)){
            intent = new Intent(QrCodeActivity.this,ChargeActivity.class);
            intent.putExtra("from","QrCodeActivityBack");
        }else if ("Charge2Activity".equals(from)){
            intent = new Intent(QrCodeActivity.this,Charge2Activity.class);
            intent.putExtra("from","QrCodeActivityBack");
        }else if("returnDeviceActivity".equals(from)){
            intent = new Intent(QrCodeActivity.this,MenuActivity.class);
            intent.putExtra("from","QrCodeActivityBack");
        }else{
            return;
        }
        isStop = true;
        startActivity(intent);
        this.finish();
    }


    public void toBXHJ(View view){
        if("returnDeviceActivity".equals(from)){
            isStop = true;
            Intent intent = new Intent(QrCodeActivity.this,BXHJActivity.class);
            intent.putExtra("from","QrCodeActivityBack");
            startActivity(intent);
            this.finish();
        }


    }


}
